package cn.edu.uestc.smgt.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import cn.edu.uestc.smgt.common.StatusType;
import cn.edu.uestc.smgt.dao.AssistStudentMapper;
import cn.edu.uestc.smgt.dao.BuzhuMapper;
import cn.edu.uestc.smgt.dict.BuzhuStatusCodeType;
import cn.edu.uestc.smgt.pojo.AssistStudent;
import cn.edu.uestc.smgt.pojo.Buzhu;
import cn.edu.uestc.smgt.pojo.dto.AssistQuery;
import cn.edu.uestc.smgt.pojo.dto.BuZhuDTO;
import cn.edu.uestc.smgt.service.BuZhuService;
import cn.edu.uestc.smgt.utils.DAOResultUtil;
@Service("BuZhuService")
public class BuZhuServiceImpl implements BuZhuService {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	TransactionTemplate txTemplate;  
	@Autowired
	BuzhuMapper buzhuMapper;
	@Autowired
	AssistStudentMapper assistStudentMapper;
	public int add(Buzhu buzhu) {
		if(buzhu == null || buzhu.getStuId() == null || buzhu.getHelpDate() == null){
			return StatusType.OBJECT_NULL.getVal();
		}
		int rows = 0,exist = 0;
		//judge if exist same key(student_id && help_date)
		try{
			 exist = buzhuMapper.existByStuIdAndHelpDate(buzhu);
			if(exist > 0)
				return StatusType.EXISTS.getVal();
			buzhu.setStatusCode(BuzhuStatusCodeType.NEW.getVal());
			rows = buzhuMapper.insertSelective(buzhu);
		}catch(Exception e){
			logger.error("调用BuZhuServiceImpl.batchUpdate出错,exist={},rows={},error={}",new Object[]{exist,rows,e});
		}
		return DAOResultUtil.getAddUpDateRemoveResult(rows, 0).getVal();
	}

	public int batchAdd(List<Buzhu> buzhuList) {
		throw new UnsupportedOperationException("批量插入不支持");
	}

	public int batchUpdate(List<Buzhu> buzhuList) {
		if(buzhuList == null)
			return StatusType.OBJECT_NULL.getVal();
		final List<Buzhu> modifiedList = buzhuList;
		int rows = 0;
		try{
			rows = txTemplate.execute(new TransactionCallback<Integer>(){
				public Integer doInTransaction(TransactionStatus status) {
					int totalRows = 0;
					for(Buzhu buzhu : modifiedList){
						buzhu.setStatusCode(BuzhuStatusCodeType.REVIEW.getVal());
						totalRows += buzhuMapper.updateByPrimaryKeySelective(buzhu);
					}
					return totalRows;
				}
			});
		}catch(Exception e){
			logger.error("调用BuZhuServiceImpl.batchUpdate出错,rows={},buzhuList={},error={}",new Object[]{rows,buzhuList,e});
		}
		return DAOResultUtil.getBatchResult(rows, modifiedList.size()).getVal();
	}

	public int remove(int id) {
		int rows = 0;
		try{
			rows= this.buzhuMapper.deleteByPrimaryKey(id);
		}catch(Exception e){
			logger.error("调用BuZhuServiceImpl.remove出错,id={},rows={},error={}",new Object[]{id,rows,e});
		}
		return DAOResultUtil.getAddUpDateRemoveResult(rows, 0).getVal();
	}

	public List<BuZhuDTO> getBuzhuByPage(int startPage, int pageNum,
			int statusCode) {
		throw new UnsupportedOperationException("功能不支持");
	}

	public int batchUpdate(String ids) {
		String[] idsArray = ids.split(",");
		if(idsArray == null || idsArray.length == 0)
			return StatusType.DATA_PARSE_ERROR.getVal();
		List<Buzhu> buzhuList = new ArrayList<Buzhu>();
		for(String id : idsArray){
			buzhuList.add(new Buzhu(Integer.parseInt(id)));
		}
		return batchUpdate(buzhuList);
	}

}
